package product

import (
	"API/client"
	"API/request"
	"API/response"
	"common/utils"
	"github.com/gin-gonic/gin"
	product "product_server/proto"
)

// CreateCatProduct 商品选购(加入购物车)
func CreateCatProduct(c *gin.Context) {
	var data request.ProductCreateCatRequest

	if err := c.ShouldBind(&data); err != nil {
		response.CurrencyErrResponse(c, 400, err.Error())
		return
	}
	userId := c.GetUint("userId")
	car, err := client.ProductCreateCat(c, &product.ProductCreateCatRequest{
		Uid:       int64(userId),
		ProductId: data.ProductId,
		Num:       data.Num,
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, utils.CutOutRpcError(err.Error()))
		return
	}
	response.CurrencySuccessResponse(c, "添加购物车成功", map[string]interface{}{"car": car})
}

// DelCarProduct 从购物车中移除商品
func DelCarProduct(c *gin.Context) {
	var data request.DelCarProductRequest
	if err := c.ShouldBind(&data); err != nil {
		response.CurrencyErrResponse(c, 400, err.Error())
		return
	}
	userId := c.GetUint("userId")
	car, err := client.DelCarProduct(c, &product.DelCarProductRequest{
		Uid:       int64(userId),
		ProductId: data.ProductId,
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, utils.CutOutRpcError(err.Error()))
		return
	}
	response.CurrencySuccessResponse(c, "移除购物车商品成功", map[string]interface{}{"car": car})
}

// UpdateCarProduct 修改购物车中商品的数量
func UpdateCarProduct(c *gin.Context) {
	var data request.UpdateCarProductRequest
	if err := c.ShouldBind(&data); err != nil {
		response.CurrencyErrResponse(c, 400, err.Error())
		return
	}
	userId := c.GetUint("userId")

	car, err := client.UpdateCarProduct(c, &product.UpdateCarProductRequest{
		Uid:       int64(userId),
		ProductId: data.ProductId,
		Num:       data.Num,
		Govern:    data.Govern,
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, utils.CutOutRpcError(err.Error()))
		return
	}
	response.CurrencySuccessResponse(c, "修改购物车商品数量成功", map[string]interface{}{"car": car})
}

// DelCar 清空购物车
func DelCar(c *gin.Context) {
	userId := c.GetUint("userId")
	car, err := client.DelCar(c, &product.DelCarRequest{
		Uid: int64(userId),
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, utils.CutOutRpcError(err.Error()))
		return
	}
	response.CurrencySuccessResponse(c, "清空购物车成功", map[string]interface{}{"car": car})
}

// CarList 购物车列表展示
func CarList(c *gin.Context) {
	userId := c.GetUint("userId")
	car, err := client.CarList(c, &product.CarListRequest{
		Uid: int64(userId),
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, utils.CutOutRpcError(err.Error()))
		return
	}
	response.CurrencySuccessResponse(c, "展示成功", map[string]interface{}{"car": car})
}

// CheckoutCar 购物车结算
func CheckoutCar(c *gin.Context) {
	var req request.CheckoutCarRequest
	if err := c.ShouldBind(&req); err != nil {
		response.CurrencyErrResponse(c, 400, err.Error())
		return
	}
	userId := c.GetUint("userId")
	res, err := client.CheckoutCar(c, &product.CheckoutCarRequest{
		Uid:        int64(userId),
		ProductIds: req.ProductIds, // 支持多商品ID
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, utils.CutOutRpcError(err.Error()))
		return
	}
	response.CurrencySuccessResponse(c, "结算成功，正在处理订单...", map[string]interface{}{"success": res.Success})
}
